//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    latest:3,
    teb:3
  },
  /**
   * 跳转页面
   */
  toPage: function(e){
    var targetUrl = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: targetUrl
    })
  },
  //推荐/上新切换
  change:function(e){
    var teb = e.currentTarget.dataset.index;
    this.setData({
      teb:teb
    })
    wx.request({
      url: '' + url + '/shop/goods/search2',
      data: { word: '', page: 1, sort: '', latest: teb },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data.Rows.length > 0) {
          var glist = res.data.Rows
          that.setData({
            glist: glist
          })
        } else {
          wx.showToast({
            title: '亲，没有符合条件的商品哦',
          })
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  //每日推荐/上新
  search: function (words, page, sort, latest, that,) {
    var url = app.data.url
    wx.request({
      url: '' + url + '/shop/goods/search2',
      data: { word: words, page: 1, sort: sort, latest:latest },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data.Rows.length > 0) {
          var glist = res.data.Rows
          that.setData({
            glist: glist
          })
        } else {
          wx.showToast({
            title: '亲，没有符合条件的商品哦',
          })
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  },
  onLoad: function () {
    var url=app.data.url
    var that = this
    that.setData({
      url: url
    })
    that.search('',1,'',3,that)
    //每日新闻
    wx.request({
      url: '' + url + '/Shop/Article/List',
      data: '',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var infolist = res.data.Rows
        var length=infolist.length
        var length2 = parseInt(length/3)
        that.setData({
          infolist: infolist,
          length:length2
        })
      },
      error: function (res) {
        console.log(res)
      }
    })
    //轮播图
    wx.request({
      url: '' + url + '/shop/index/getslide',
      data: '',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var banlist = res.data.Rows
        console.log(res)
        that.setData({
          imgUrls: banlist
        })
      },
      error: function (res) {
        console.log(res)
      }
    })
  }
});

